var Model = function(location, options){
	this._location = location;
}

Model.prototype._location = '';
Model.prototype._request = null;
Model.prototype._currentpage = 0;
Model.prototype._options = {
	'type':'json'
};
Model.prototype._cache = [];
Model.prototype._callbackStack = [];

Model.prototype.get = function(length,offset,fn){
	var request = new Model.Request(this,fn,length,offset);
	request.get();
}

Model.prototype._buffer = function(result,length,offset){
	var total = (length+offset);
	for(var i = offset; i <= total; ++i){
		this._cache[i] = result[i];
	}
}
Model.prototype._isBufferd = function(length,offset){
	var total = (length+offset);
	for(var i = offset; i < total; ++i){
		if(!this._cache[i]){ return false; }
	}
	return true;
}



Model.Request = function(scope,fn,length,offset){
	this.length = length;
	this.offset = offset;
	this.fn = fn;
	this.scope = scope;
	this._request = new Request.JSON({'url': this.scope._location });
}

Model.Request.prototype.get = function(){
	if( this.scope._isBufferd(this.length,this.offset) ){
		console.debug(this.scope);
		this.fn( this.scope._cache.offsetGet(this.length,this.offset) );
	} else {
		this._request.onSuccess = this._onSuccess.bind(this);
		this._request.get({'length':this.length,'offset':this.offset});
	}
}

Model.Request.prototype._onSuccess = function(result){
	this.scope._buffer(result,this.length,this.offset);
	this.fn(result);
}


Array.prototype.offsetGet = function(length,offset){
	var arr = new Array(length);
	var total = (length+offset);
	var b = 0;
	for(var i = offset; i <= total; ++i){
		if(this[i]){ arr[b] = this[i]; }
		++b;
	}
	
	return arr;
}